/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hitunggaji;

import Koneksi.KoneksiDatabase;
import UpdateKaryawan.updateKaryawanKontrol;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import Karyawan.Karyawan;
import updatepotongan.potongan;

/**
 *
 * @author yolan
 */
public class gajikontrol {

    private Connection conn;

    public gajikontrol(Connection conn2) {
        this.conn = conn2;
    }

    public static gajikontrol getKoneksi() throws SQLException {
        KoneksiDatabase konDB = new KoneksiDatabase();
        gajikontrol katkontrol;
        katkontrol = new gajikontrol(KoneksiDatabase.getConn());
        return katkontrol;
    }

    public List<Gaji> getNamaBarang(String nama) throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        conn.setAutoCommit(true);

        String sql = "select g.gajigolongan, p.pinjaman,p.nip  "
                + "from gaji g, potongan p "
                + "where g.nip = p.nip "
                + "AND p.nip LIKE '" + nama + "%' "
                + "order by p.nip";
        pstmt = conn.prepareStatement(sql);
        rset = pstmt.executeQuery();
        List<Gaji> b = new ArrayList<Gaji>();

        while (rset.next()) {
            Gaji barang = new Gaji();
            potongan kategori = new potongan();
            Karyawan k=new Karyawan();

            barang.setGajigolongan(rset.getDouble(1));
            barang.setPinjaman(kategori);
            kategori.setPinjaman(rset.getDouble(2));
            
            barang.setNip(k);
           k.setNIP(rset.getString(3));

           
           
            double saldolama = rset.getDouble(1) - rset.getDouble(2);
            barang.setTotalgaji(saldolama);

            b.add(barang);
        }
        conn.commit();

        return b;
    }

    public List<Gaji> getAllKategori(String nip) throws SQLException {
        PreparedStatement psmt = null;
        ResultSet rset = null;
        conn.setAutoCommit(false);
        String sql = "select * from detailgaji";
        psmt = conn.prepareStatement(sql);
        rset = psmt.executeQuery();
        List<Gaji> st = new ArrayList<Gaji>();

        while (rset.next()) {
            Gaji stok = new Gaji();
            potongan k = new potongan();
            Karyawan hadir = new Karyawan();
            k.setPinjaman(rset.getDouble(1));

            stok.setPinjaman(k);

            stok.setGajigolongan(rset.getDouble(2));
            hadir.setJumlahhadir(rset.getString(3));
            stok.setTotalgaji(rset.getDouble(4));

            stok.setJumlahhadir(hadir);

            double saldolama = rset.getDouble(1) - rset.getDouble(2);
           stok.setTotalgaji(saldolama);

            st.add(stok);
            saldolama = 0;
        }
        conn.commit();
        return st;
    }
//        PreparedStatement pstmt = null;
//        ResultSet rset = null;
//        conn.setAutoCommit(false);
//
//        String sql = "select g.gajigolongan, p.pinjaman,p.nip  "
//                + "from gaji g, potongan p "
//                + "where g.nip = p.nip "
//                + "AND p.nip LIKE '" + nip + "%' "
//                + "order by p.nip";
//        pstmt = conn.prepareStatement(sql);
//        rset = pstmt.executeQuery();
//        List<Gaji> b = new ArrayList<Gaji>();
//
//        while (rset.next()) {
//            Gaji barang = new Gaji();
//            potongan kategori = new potongan();
//            Karyawan k=new Karyawan();
//
//            barang.setGajigolongan(rset.getDouble(1));
//            barang.setPinjaman(kategori);
//            kategori.setPinjaman(rset.getDouble(2));
//            
//            barang.setNip(k);
//           k.setNIP(rset.getString(3));
//
//           barang.setJumlahhadir(k);
//           k.setJumlahhadir(rset.getString(4));
//           
//            double saldolama = (rset.getDouble(1) - rset.getDouble(2))*rset.getDouble(4);
//            barang.setTotalgaji(saldolama);
//
//            b.add(barang);
//        }
//        conn.commit();
//
//        return b;
//    }
}
